package com.oauth.resource.config;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.ObjectUtil;
import com.secure.annotation.ApiSecure;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.ApplicationContext;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Slf4j
public class ApplicationContextHolder {

    private static ApplicationContext applicationContext;
    private static RequestMappingHandlerMapping requestMappingHandlerMapping;
    private static final List<String> secureApiPathList = new ArrayList<>();

    public static RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
        return requestMappingHandlerMapping;
    }

    public static void setRequestMappingHandlerMapping(RequestMappingHandlerMapping requestMappingHandlerMapping) {
        ApplicationContextHolder.requestMappingHandlerMapping = requestMappingHandlerMapping;
        filterApiSecureServletPathList(requestMappingHandlerMapping);
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static void setApplicationContext(ApplicationContext applicationContext) {
        ApplicationContextHolder.applicationContext = applicationContext;

        String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
        for (String beanDefinitionName : beanDefinitionNames) {
            Object bean = applicationContext.getBean(beanDefinitionName);
            log.info("bean：{}",bean.getClass());
            boolean annotationPresent = ClassUtil.getClass(bean).isAnnotationPresent(FeignClient.class);
            if(annotationPresent){
                log.info("feignclient：{}",bean.getClass());
            }
        }
    }

    /**
     * 过滤需要api安全校验的处理器方法路径
     *
     * @param requestMappingHandlerMapping
     */
    private static void filterApiSecureServletPathList(RequestMappingHandlerMapping requestMappingHandlerMapping) {
        if (ObjectUtil.isNotNull(requestMappingHandlerMapping)) {
            Map<RequestMappingInfo, HandlerMethod> handlerMethods = requestMappingHandlerMapping.getHandlerMethods();
            if (CollectionUtil.isNotEmpty(handlerMethods)) {
                for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : handlerMethods.entrySet()) {
                    RequestMappingInfo requestMappingInfo = entry.getKey();
                    HandlerMethod handlerMethod = entry.getValue();
                    if (handlerMethod.hasMethodAnnotation(ApiSecure.class)) {
                        secureApiPathList.addAll(requestMappingInfo.getPatternValues());
                    }
                }
            }
        }
    }

    public static List<String> getApiSecureServletPathList() {
        return secureApiPathList;
    }

    /**
     * 获取处理器方法
     *
     * @param servletPath
     * @return
     */
    public static HandlerMethod getHandlerMethod(String servletPath) {
        if (ObjectUtil.isNotNull(requestMappingHandlerMapping)) {
            Map<RequestMappingInfo, HandlerMethod> handlerMethods = requestMappingHandlerMapping.getHandlerMethods();
            if (CollectionUtil.isNotEmpty(handlerMethods)) {
                Optional<HandlerMethod> handlerMethodOptional = handlerMethods.entrySet().stream().filter(entry -> entry.getKey().getPatternValues().contains(servletPath)).map(entry -> entry.getValue()).findFirst();
                if (handlerMethodOptional.isPresent()) {
                    return handlerMethodOptional.get();
                }
            }
        }
        return null;
    }
}
